package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.sg.own.entity.CommunicationTerminal;
import com.xx.sg.own.service.CommunicationTerminalService;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/communicationTerminal")
@Tag(name = "对讲机", description = "对讲机API")
public class CommunicationTerminalController {

    @Autowired
    private CommunicationTerminalService terminalService;

    @GetMapping("/devInfo/{devId}")
    @Operation(summary = "根据设备ID获取最新信息", description = "根据设备ID获取最新信息")
    public CommunicationTerminal devInfo(@PathVariable("devId") String devId) {
        return terminalService.selectCommunicationTerminalByDevId(devId);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "根据ID获取详细信息", description = "根据ID获取详细信息")
    public CommunicationTerminal info(@PathVariable("id") String id) {
        return terminalService.selectCommunicationTerminalById(id);
    }

    @GetMapping("/list")
    @Operation(summary = "获取对讲机", description = "获取对讲机")
    public List<CommunicationTerminal> list(@RequestParam(required = false) CommunicationTerminal terminal) {
        return terminalService.selectCommunicationTerminalList(terminal);
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取对讲机", description = "分页获取对讲机")
    public Page<CommunicationTerminal> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) CommunicationTerminal terminal) {
        Page<CommunicationTerminal> page = new Page<>(current, size);
        return terminalService.selectCommunicationTerminalPage(page, terminal);
    }

    @GetMapping("/page1")
    @Operation(summary = "分页获取对讲机1", description = "分页获取对讲机1")
    public R page1(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @ModelAttribute CommunicationTerminal terminal) {
        Page<CommunicationTerminal> page = new Page<>(current, size);
        terminalService.selectCommunicationTerminalPage(page, terminal);
        R r = R.success(page);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @PostMapping("/bindUser")
    @Operation(summary = "绑定用户", description = "绑定用户")
    public R bindUser(@RequestBody CommunicationTerminal terminal) {
        LambdaUpdateWrapper<CommunicationTerminal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CommunicationTerminal::getDevId, terminal.getDevId())
                .set(CommunicationTerminal::getUserId, terminal.getUserId());
        terminalService.update(wrapper);
        R r = R.success();
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @PostMapping("/add")
    @Operation(summary = "添加对讲机", description = "添加对讲机")
    public R add( CommunicationTerminal terminal) {
        terminalService.save(terminal);
        R r = R.success(terminal);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @PostMapping("/modify")
    @Operation(summary = "修改对讲机", description = "修改对讲机")
    public R modify(CommunicationTerminal terminal) {
        terminalService.updateById(terminal);
        R r = R.success(terminal);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @GetMapping("/info1/{id}")
    @Operation(summary = "根据ID获取详细信息", description = "根据ID获取详细信息")
    public R info1(@PathVariable("id") String id) {
        CommunicationTerminal terminal = terminalService.selectCommunicationTerminalById(id);
        R r = R.success(terminal);
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除对讲机信息", description = "删除对讲机信息")
    public R delete(@PathVariable("id") String id) {
        terminalService.removeById(id);
        R r = R.success();
        r.put("data", "成功");
        r.put("message", "成功");
        r.put("success", true);
        return r;
    }

    @GetMapping("/range")
    @Operation(summary = "范围内对讲机信息", description = "范围内对讲机信息")
    public R range(@RequestParam String longitude,
                   @RequestParam String latitude,
                   @RequestParam String radius){
        LambdaQueryWrapper<CommunicationTerminal> wrapper = new LambdaQueryWrapper<>();

        wrapper.apply(
                "ST_DWithin(ST_MakePoint(longitude::double precision, latitude::double precision)::geography, " +
                        "ST_MakePoint({0}::double precision, {1}::double precision)::geography, {2}::double precision)",
                longitude, latitude, radius
        );
        List<CommunicationTerminal> list = terminalService.list(wrapper);
        return R.success(list);
    }
}
